package bll

import (
	"7qule.com/sd3/CrossServer/src/bll/kuangzhan/dal"
	. "7qule.com/sd3/CrossServer/src/bll/kuangzhan/model"
	"7qule.com/sd3/CrossServer/src/bll/world"
)

// 镖局信息务逻辑类
type globalKuangzhanBll struct {
	// 镖局信息数据
	data map[int32]*GlobalKuangzhanLayer
}

// --------------------------------初始化--------------------------------------------------

// 镖局信息逻辑类
var GlobalKuangzhanBll = &globalKuangzhanBll{}

// 初始化
func init() {
	// 注册数据初始化函数
	world.CompleteMgr.RegisterFunc(GlobalKuangzhanBll.StructName(), GlobalKuangzhanBll.initData)
}

// StructName 模块名
func (this *globalKuangzhanBll) StructName() string {
	return "GlobalKuangzhanBll"
}

//
// initData
// @Description:初始化数据
// @receiver this
// @return errList:错误信息
//
func (this *globalKuangzhanBll) initData() (errList []error) {

	tempList, err := dal.GlobalKuangzhanDal.GetData()
	if err != nil {
		errList = append(errList, err)
		return
	}

	tempMap := make(map[int32]*GlobalKuangzhanLayer)

	if len(tempList) == 0 {
		for layerKey, layer := range BaseKuangzhanModelBll.GetData() {
			for positionKey, position := range layer {
				data := &GlobalKuangzhan{
					LayerId:    position.LayerId,
					PositionId: position.PositionId,
				}
				GlobalKuangzhanBll.insert(data)
				tempLayer, ok := tempMap[layerKey]
				if !ok {
					tempLayer = NewGlobalKuangzhanPosition()
				}
				tempLayer.AddPosition(positionKey, data)
				tempMap[layerKey] = tempLayer
			}
		}
		this.data = tempMap
		return
	}

	for _, item := range tempList {
		tempLayer, ok := tempMap[item.LayerId]
		if !ok {
			tempLayer = NewGlobalKuangzhanPosition()
		}
		tempLayer.AddPosition(item.PositionId, item)
		tempMap[item.LayerId] = tempLayer
	}
	this.data = tempMap
	this.check()
	this.check2()
	GlobalKuangzhanPlayer.InitData()
	GlobalKuangzhanGuild.InitData()
	return
}

// --------------------------------生成的基础方法--------------------------------------------------

//
// GetData
// @Description:获取数据
// @receiver this
// @return map[int32]*GlobalKuangzhanLayer:获取到的镖局信息数据
//
func (this *globalKuangzhanBll) GetData() map[int32]*GlobalKuangzhanLayer {
	return this.data
}

// --------------------------------内部方法--------------------------------------------------

//
// insert
// @Description: 插入数据
// @receiver this
// @param data:数据对象
//
func (this *globalKuangzhanBll) insert(data *GlobalKuangzhan) {
	dal.GlobalKuangzhanDal.Insert(data)
}

//
// update
// @Description:更新数据
// @receiver this
// @param data:数据对象
//
func (this *globalKuangzhanBll) update(data *GlobalKuangzhan) {
	dal.GlobalKuangzhanDal.Update(data)
}

//
// delete
// @Description:删除数据
// @receiver this
// @param data:数据对象
//
func (this *globalKuangzhanBll) delete(data *GlobalKuangzhan) {
	dal.GlobalKuangzhanDal.Delete(data)
}

//
// deleteLayer
// @Description:删除一层数据
// @receiver this
// @param LayerId:层数id
//
func (this *globalKuangzhanBll) deleteLayer(LayerId int32) {
	dal.GlobalKuangzhanDal.DeleteLayer(LayerId)
}

//
// getNode
// @Description:获取节点信息
// @receiver this
// @param layerId:层数id
// @param positionId:节点id
// @return result:节点
// @return ok:节点是否存在
//
func (this *globalKuangzhanBll) getNode(layerId, positionId int32) (result *GlobalKuangzhan, ok bool) {
	position, ok := this.data[layerId]
	if ok {
		result, ok = position.GetPosition(positionId)
	}
	return
}

//
// check
// @Description:检查B表是否有新增数据
// @receiver this
//
func (this *globalKuangzhanBll) check() {
	for layerKey, layer := range BaseKuangzhanModelBll.GetData() {
		for positionKey := range layer {
			kuangzhanPosition, ok := this.data[layerKey]
			if !ok {
				kuangzhanPosition = NewGlobalKuangzhanPosition()
				this.data[layerKey] = kuangzhanPosition
			}
			_, ok = kuangzhanPosition.GetPosition(positionKey)
			if !ok {
				data := &GlobalKuangzhan{
					LayerId:    layerKey,
					PositionId: positionKey,
				}
				this.insert(data)
				kuangzhanPosition.AddPosition(positionKey, data)
			}
		}
	}
}

//
// check2
// @Description:检查B表是否有删除数据
// @receiver this
//
func (this *globalKuangzhanBll) check2() {
	for layerKey, layer := range this.data {
		_, ok := BaseKuangzhanModelBll.data[layerKey]
		if !ok {
			this.deleteLayer(layerKey)
			delete(this.data, layerKey)
			continue
		}
		for positionKey, position := range layer.GetData() {
			_, ok = BaseKuangzhanModelBll.data[layerKey][positionKey]
			if !ok {
				this.delete(position)
				this.data[layerKey].Delete(positionKey)
			}
		}
	}
}

// --------------------------------外部方法--------------------------------------------------

//
// GetLayerItem
// @Description:获取数据
// @receiver this
// @param layerId:层数id
// @return result:层数详情
// @return exist:层数是否存在
//
func (this *globalKuangzhanBll) GetLayerItem(layerId int32) (result map[int32]*GlobalKuangzhan, exist bool) {
	layer, exist := this.data[layerId]
	result = layer.GetData()
	return
}

//
// GetPlayerItem
// @Description:获取用户占领的节点信息
// @receiver this
// @param playerId:用户id
// @return result:节点信息
// @return exist:是否占领
//
func (this *globalKuangzhanBll) GetPlayerItem(playerId string) (result *GlobalKuangzhan, exist bool) {
	result, exist = GlobalKuangzhanPlayer.GetPlayer(playerId)

	return
}

//
// GetPositionIsHavePerson
// @Description:获取节点是否有人占领
// @receiver this
// @param layerId:层数id
// @param positionId:节点id
// @return result:节点信息
// @return exist:是否有人占领
//
func (this *globalKuangzhanBll) GetPositionIsHavePerson(layerId, positionId int32) (result *GlobalKuangzhan, exist bool) {
	result, _ = this.getNode(layerId, positionId)
	if result.PlayerId != "" {
		return result, true
	}
	return
}

//
// UpdateInfo
// @Description: 更新数据
// @receiver this
// @param playerId 用户id
// @param layerId 层数id
// @param positionId 节点id
// @param guildId 工会id
//
func (this *globalKuangzhanBll) UpdateInfo(layerId, positionId int32, playerId string, guildId string) *GlobalKuangzhan {
	data, _ := this.getNode(layerId, positionId)
	data.LayerId = layerId
	data.PositionId = positionId
	data.PlayerId = playerId
	data.GuildId = guildId
	this.update(data)
	return data
}

//
// GetKuangzhanGangTerritoryInfo
// @Description: 获取帮派镖局数据
// @receiver this
// @param guildId 工会id
// @return []gangTerritory
//
func (this *globalKuangzhanBll) GetKuangzhanGangTerritoryInfo(guildId string) (result map[int32]int32, exist bool) {
	result, exist = GlobalKuangzhanGuild.GetGangTerritory(guildId)
	return
}

//
// GetKuangzhanGangOverview
// @Description: 获取镖局总览
// @receiver this
// @return map[int32]map[int32]*GlobalKuangzhan
//
func (this *globalKuangzhanBll) GetKuangzhanGangOverview() map[int32]map[int32]*GlobalKuangzhan {
	temp := make(map[int32]map[int32]*GlobalKuangzhan)
	for layerKey := range this.data {
		for positionKey, position := range this.data[layerKey].GetData() {
			layer := temp[layerKey]
			if layer == nil {
				layer = make(map[int32]*GlobalKuangzhan)
			}
			layer[positionKey] = position
			temp[layerKey] = layer
		}
	}
	return temp
}
